package com.bb.blog.code.threading;

import com.bb.blog.code.threading.example.atomic.UnsafeExample;
import org.openjdk.jol.info.ClassLayout;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * JVM 内置锁的升级过程
 */
public class Synchronized {

    /**
     * 01 无锁
     * <p>
     * <p>
     * <p>
     * 01 偏向锁
     * 只有单一线程执行代码块
     * <p>
     * 00 轻量级锁
     * 线程之间交替执行场景，竞争程度不是很激烈
     * 例如 A 在执行同步块  B准备执行
     * 那么B会自旋去等待A获取锁，自旋不会丢弃CPU使用权
     * 自旋: 设定初始自旋次数后，会自动去进行找到一个合理的自旋次数
     * <p>
     * 10 重量级锁  需要向cpu申请Mutex互斥量
     * <p>
     * <p>
     * -XX:+UseBiasedLocking -XX:BiasedLockingStartupDelay=0
     */


    public static void main(String[] args) throws InterruptedException {

        Object myObject = new Object();
        System.out.println("当前线程：" + Thread.currentThread().getName() + "--" + ClassLayout.parseInstance(myObject).toPrintable());
//        Thread.sleep(5000);
//        synchronized (myObject) {
//            System.out.println("我获得了锁");
//            System.out.println("当前线程：" + Thread.currentThread().getName() + "--" + ClassLayout.parseInstance(myObject).toPrintable());
//            System.out.println("我获得锁222");
//
//        }

        Thread.sleep(7000);
        Executor executor = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 3; i++) {
//            executor.execute(new Runnable() {
//                @Override
//                public void run() {
////                    while(true){
////                        UnsafeExample.getUnsafe().monitorEnter(myObject);
//                        synchronized (myObject) {
//                            System.out.println("当前线程：" + Thread.currentThread().getName() + "--" + ClassLayout.parseInstance(myObject).toPrintable());
//
//                        }
////                        UnsafeExample.getUnsafe().monitorExit(myObject);
////                        try {
////                            Thread.sleep(3000);
////                        } catch (InterruptedException e) {
////                            e.printStackTrace();
////                        }
////                    }
//                }
//            } );
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
//                    while(true){
//                    UnsafeExample.getUnsafe().monitorEnter(myObject);
                    synchronized (myObject) {
                        System.out.println("当前线程：" + Thread.currentThread().getName() + "--" + ClassLayout.parseInstance(myObject).toPrintable());

                    }
//                    UnsafeExample.getUnsafe().monitorExit(myObject);
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
//                    }
                }
            });
            t.start();
            Thread.sleep(50000);
        }
    }

}
